/**HEADER********************************************************************
* 
* Copyright (c) 2008 Freescale Semiconductor;
* All Rights Reserved
*
* Copyright (c) 2004-2010 Embedded Access Inc.;
* All Rights Reserved
*
* Copyright (c) 1989-2008 ARC International;
* All Rights Reserved
*
*************************************************************************** 
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
* THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************
*
* $FileName: mpc83xx_etsec_mii.c$
* $Version : 3.8.1.0$
* $Date    : May-12-2011$
*
* Comments:
*
*   This file initializes the 83xx ethernet interface.
*
*END************************************************************************/

#include <mqx.h>
#include <bsp.h>
#include <bsp_prv.h>
#include <ethernet.h>
#include <enet.h>
#include <enetprv.h>
#include <mdio.h>
#include <mpc83xx_etsec_prv.h>

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_get_speed
*  Returned Value : uint_32 - 10, 100 or 1000
*  Comments       :
*    Determines the Ethernet speed in Mbps
*
*END*-----------------------------------------------------------------*/

uint_32 MPC83xx_etsec_get_speed
   (
      /* [IN] the Ethernet state structure */
      ENET_CONTEXT_STRUCT_PTR enet_ptr
   )
{ 
   uint_32                 speed = 10, tmp, tmp2;

   /* Read PHY 1000Base-T Status */
   MPC83xx_etsec_read_mdio(enet_ptr, MDIO_GBSR, &tmp, MPC83XX_TSEC_DEFAULT_PHY_WAIT);
   if (tmp & (MDIO_GB_LNK_1000BT_FULL | MDIO_GB_LNK_1000BT_HALF)) {
   	 return 1000;
   }
  
   /* Read PHY control register */
   MPC83xx_etsec_read_mdio(enet_ptr, MDIO_BMCR, &tmp, MPC83XX_TSEC_DEFAULT_PHY_WAIT);

   if (tmp & MDIO_CTRL_AUTONEG) {
      /* Auto Negotiation enabled */
      /* Determine speed */
      MPC83xx_etsec_read_mdio(enet_ptr, MDIO_ANLPAR, &tmp2, MPC83XX_TSEC_DEFAULT_PHY_WAIT);
      if (tmp2 & (MDIO_AUTO_LNK_100BASE_T4 | MDIO_AUTO_LNK_100BASE_TX_FULL | MDIO_AUTO_LNK_100BASE_TX)) {
         speed = 100;
      } 
   } else { 
      /* Auto Negotiation disabled */
      if (tmp & MDIO_CTRL_SPEED_1) {
         speed = 100;
      }
   } 

   return speed;
} 



/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_read_mdio
*  Returned Value : boolean
*  Comments       :
*     Reads the specified MDIO register.
*
*END*-----------------------------------------------------------------*/

uint_32 MPC83xx_etsec_read_mdio
   (
      /* [IN] the Ethernet state structure */
      ENET_CONTEXT_STRUCT_PTR enet_ptr,
      /* [IN] the register to read */
      uint_32                 reg,
      /* [OUT] data read */
      uint_32_ptr             data_ptr,
      /* [IN] timeout to wait for data */
      uint_32                 timeout
   )
{
   MPC83XX_TSEC_CONTEXT_STRUCT_PTR  tsec_context_ptr;
   MPC83xx_ETSEC_STRUCT_PTR   tsec_ptr;
   boolean forever = FALSE;
    
   tsec_context_ptr = enet_ptr->MAC_CONTEXT_PTR;
   tsec_ptr = tsec_context_ptr->TSEC_ADDRESS;

   if (!timeout) {
      forever = TRUE;
   } 
   while (timeout-- || forever) {
      if ((tsec_ptr->MIIMIND & (MPC83xx_TSEC_MIIMIND_READ_NOT_VALID   |
                                   MPC83xx_TSEC_MIIMIND_SCAN_IN_PROGRESS |
                                   MPC83xx_TSEC_MIIMIND_BUSY)) == 0)
      {
         break;
      }
      if (timeout == 0) {
         return FALSE;
      }
      _time_delay(1);
   }
   
   tsec_ptr->MIIMADD = ((enet_ptr->PHY_ADDRESS & 0x1F) << 8) | (reg & 0x1F);
   _PSP_SYNC();

   /* Issue read command */
   tsec_ptr->MIIMCOM = 0;
   _PSP_SYNC();
   tsec_ptr->MIIMCOM = MPC83xx_TSEC_MIIMCOM_READ;
   _PSP_SYNC();

   /* Wait for read command to complete */
   while (timeout-- || forever) {
      if ((tsec_ptr->MIIMIND & MPC83xx_TSEC_MIIMIND_BUSY) == 0) {
         break;
      }
      if (timeout == 0) {
         return FALSE;
      }
      _time_delay(1);
   }
  
   /* Clear read command, its not self clearing */
   tsec_ptr->MIIMCOM &= ~MPC83xx_TSEC_MIIMCOM_READ;
   _PSP_SYNC();


   *data_ptr = tsec_ptr->MIIMSTAT;
   /* Get the data */
   return TRUE;
}


/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_write_mdio
*  Returned Value : boolean
*  Comments       :
*     Writes to the specified MDIO register
*
*END*-----------------------------------------------------------------*/

uint_32 MPC83xx_etsec_write_mdio
   (
      /* [IN] the Ethernet state structure */
      ENET_CONTEXT_STRUCT_PTR  enet_ptr,
      /* [IN] the register to write to */
      uint_32              reg,
      /* [IN] the data to write */
      uint_32              data,
      /* [IN] timeout */
      uint_32              timeout
   )
{
   MPC83XX_TSEC_CONTEXT_STRUCT_PTR  tsec_context_ptr;
   MPC83xx_ETSEC_STRUCT_PTR          tsec_ptr;
   
   tsec_context_ptr = enet_ptr->MAC_CONTEXT_PTR;
   tsec_ptr = tsec_context_ptr->TSEC_ADDRESS;
   
   /* Wait for previous command to complete */
   if (!timeout) {
      timeout = 1000;
   } 
   while (timeout) {
      if ((tsec_ptr->MIIMIND & (MPC83xx_TSEC_MIIMIND_READ_NOT_VALID   |
                                   MPC83xx_TSEC_MIIMIND_SCAN_IN_PROGRESS |
                                   MPC83xx_TSEC_MIIMIND_BUSY)) == 0)
      {
         break;
      }
      timeout--;

      if (timeout == 0) {
         return FALSE;
      }
      _time_delay(1);
   }
   
   // Write register
   tsec_ptr->MIIMADD = ((enet_ptr->PHY_ADDRESS & 0x1F) << 8) | (reg & 0x1F);
   _PSP_SYNC();
   
   /* Write Data */
   tsec_ptr->MIIMCON = data & MPC83xx_TSEC_MIIMCON_PHY_WRITE_MASK;
   _PSP_SYNC();

   /* Wait for command to complete */
   while (tsec_ptr->MIIMIND & MPC83xx_TSEC_MIIMIND_BUSY) {
      _time_delay(1);
   }      
   
   return TRUE;
}
